Code
# Import libraries
import os
import pandas as pd
import numpy as np
import plotly.express as pxIn this notebook, we carry out an in-depth exploratory and descriptive analysis of the UCI Adult Income Dataset, a widely used dataset for income prediction tasks based on individual demographic and employment attributes.
This phase of analysis is essential for uncovering patterns, detecting potential biases, and gaining intuition about the dataset’s structure before applying any modelling procedures. We examine the distribution of key numerical and categorical variables, investigate relationships between demographic features and income levels, and use visualizations to summarize insights. Particular focus is placed on income disparities across age groups, geographical regions, races, and education-occupation combinations, helping lay a solid foundation for downstream modeling and policy-relevant interpretation.
We begin our analysis by importing the core Python libraries required for data handling, numerical computation, visualization, and directory management:
pandas: Enables efficient manipulation, filtering, and aggregation of structured tabular data, forming the backbone of our analysis pipeline.
numpy: Provides support for fast numerical operations, array-based computation, and statistical routines.
os: Facilitates interaction with the file system, allowing us to construct flexible and portable directory paths for data and output management.
plotly.express: A high-level graphing library that enables the creation of interactive, publication-quality visualizations, which we use extensively to uncover patterns and present insights throughout the notebook.
# Import libraries
import os
import pandas as pd
import numpy as np
import plotly.express as pxTo ensure reproducibility andorganized storage, we programmatically create directories if they don’t already exist for:
These directories will store intermediate and final outputs for reproducibility.
We load the cleaned version of the UCI Adult Income Dataset from the processed data directory into a Pandas DataFrame. The head(10) function shows the first ten records, giving a glimpse into the data columns such as age, workclass, education_num, etc.
adult_data_filename = os.path.join(processed_dir, "adult_cleaned.csv")
adult_df = pd.read_csv(adult_data_filename)
adult_df.head(10)| age | workclass | fnlwgt | education_num | marital_status | relationship | race | sex | capital_gain | capital_loss | hours_per_week | income | education_level | occupation_grouped | native_region | age_group | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 39 | government | 77516 | 13 | single | single | white | male | 2174 | 0 | 40 | <=50k | tertiary | white collar | north america | 36-45 |
| 1 | 50 | self-employed | 83311 | 13 | married | male-spouse | white | male | 0 | 0 | 13 | <=50k | tertiary | white collar | north america | 46-60 |
| 2 | 38 | private | 215646 | 9 | divorced or separated | single | white | male | 0 | 0 | 40 | <=50k | secondary-school-graduate | blue collar | north america | 36-45 |
| 3 | 53 | private | 234721 | 7 | married | male-spouse | black | male | 0 | 0 | 40 | <=50k | secondary | blue collar | north america | 46-60 |
| 4 | 28 | private | 338409 | 13 | married | female-spouse | black | female | 0 | 0 | 40 | <=50k | tertiary | white collar | central america | 26-35 |
| 5 | 37 | private | 284582 | 14 | married | female-spouse | white | female | 0 | 0 | 40 | <=50k | tertiary | white collar | north america | 36-45 |
| 6 | 49 | private | 160187 | 5 | divorced or separated | single | black | female | 0 | 0 | 16 | <=50k | secondary | service | central america | 46-60 |
| 7 | 52 | self-employed | 209642 | 9 | married | male-spouse | white | male | 0 | 0 | 45 | >50k | secondary-school-graduate | white collar | north america | 46-60 |
| 8 | 31 | private | 45781 | 14 | single | single | white | female | 14084 | 0 | 50 | >50k | tertiary | white collar | north america | 26-35 |
| 9 | 42 | private | 159449 | 13 | married | male-spouse | white | male | 5178 | 0 | 40 | >50k | tertiary | white collar | north america | 36-45 |
adult_data_filename = os.path.join(processed_dir, "adult_cleaned.csv")
adult_df = pd.read_csv(adult_data_filename)Here, we examine the structure of the dataset:
age, hours_per_week) and categorical variables (e.g., sex, education_level).Understanding data types and null entries is essential before proceeding with analysis.
summary_df = pd.DataFrame({
'Column': adult_df.columns,
'Data Type': adult_df.dtypes.values,
'Missing Values': adult_df.isnull().sum().values
})
summary_df| Column | Data Type | Missing Values | |
|---|---|---|---|
| 0 | age | int64 | 0 |
| 1 | workclass | object | 0 |
| 2 | fnlwgt | int64 | 0 |
| 3 | education_num | int64 | 0 |
| 4 | marital_status | object | 0 |
| 5 | relationship | object | 0 |
| 6 | race | object | 0 |
| 7 | sex | object | 0 |
| 8 | capital_gain | int64 | 0 |
| 9 | capital_loss | int64 | 0 |
| 10 | hours_per_week | int64 | 0 |
| 11 | income | object | 0 |
| 12 | education_level | object | 0 |
| 13 | occupation_grouped | object | 0 |
| 14 | native_region | object | 0 |
| 15 | age_group | object | 0 |
adult_df.describe()| age | fnlwgt | education_num | capital_gain | capital_loss | hours_per_week | |
|---|---|---|---|---|---|---|
| count | 32513.000000 | 3.251300e+04 | 32513.000000 | 32513.000000 | 32513.000000 | 32513.000000 |
| mean | 38.590256 | 1.897942e+05 | 10.081629 | 1079.239812 | 87.432719 | 40.440962 |
| std | 13.638932 | 1.055788e+05 | 2.572015 | 7390.625650 | 403.243596 | 12.350184 |
| min | 17.000000 | 1.228500e+04 | 1.000000 | 0.000000 | 0.000000 | 1.000000 |
| 25% | 28.000000 | 1.178330e+05 | 9.000000 | 0.000000 | 0.000000 | 40.000000 |
| 50% | 37.000000 | 1.783560e+05 | 10.000000 | 0.000000 | 0.000000 | 40.000000 |
| 75% | 48.000000 | 2.370510e+05 | 12.000000 | 0.000000 | 0.000000 | 45.000000 |
| max | 90.000000 | 1.484705e+06 | 16.000000 | 99999.000000 | 4356.000000 | 99.000000 |
This summary provides a snapshot of key distribution characteristics. We see that:
Age ranges from 17 to 90, with a mean of 38.6 years. It is slightly right-skewed (positively skewed). While the average age is approximately 38.6 years, an examination of the percentiles reveals that the majority of individuals are clustered in the younger to middle-age range, with fewer observations in the older age brackets. This skewed age distribution might suggest labor force participation is concentrated in specific age groups, which could reflect broader demographic or economic realities.
Capital gains/losses are highly skewed, with most values at 0 (the 75th percentile is 0). This indicates that a small number of individuals report very large gains or losses, especially evident in the capital gain variable which reaches up to $99,999. These variables act as proxies for wealth-related income that goes beyond regular wages or salaries. Individuals with non-zero values for capital gains or losses often represent a distinct socioeconomic subset of the population — typically more financially literate, or with access to investment assets. The stark inequality in their distributions mirrors real-world disparities in asset ownership and investment returns.
The dataset has individuals working anywhere from 1 to 99 hours per week, with a median of 40. This aligns with the standard full-time work week in many countries (8 hours per day for 5 working days). The mean is slightly above that at 40.4 hours, suggesting a mild right skew, with a small subset of individuals working significantly longer hours. The mode is also 40, further reinforcing the prevalence of full-time work. A non-trivial number of individuals report working very few hours, possibly due to part-time work, unemployment, or semi-retirement. On the other extreme, some report working more than 45 hours per week, which may indicate multiple jobs, weekend-work, self-employment, or informal labor, and could reflect socioeconomic necessity.
workclass
adult_df['workclass'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')workclass variable showing the proportion of each unique category within the dataset.
| unique values | proportion | |
|---|---|---|
| 0 | private | 0.696644 |
| 1 | self-employed | 0.112447 |
| 2 | government | 0.069418 |
| 3 | local-gov | 0.064374 |
| 4 | unknown | 0.056470 |
| 5 | voluntary | 0.000431 |
| 6 | unemployed | 0.000215 |
The private sector dominates, employing ~69.7% of the population. The government sector (13.4%) and self-employment (11.2%) also make up substantial portions of the workforce. A small fraction is labeled as “unknown” (5.6%), which may correspond to missing or ambiguous data entries. Tiny proportions are voluntary (0.04%) or unemployed (0.02%), possibly underreported or underrepresented in the sample.
marital_status
adult_df['marital_status'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')marital_status variable.
| unique values | proportion | |
|---|---|---|
| 0 | married | 0.460862 |
| 1 | single | 0.327684 |
| 2 | divorced or separated | 0.180912 |
| 3 | widowed | 0.030542 |
Married individuals make up the largest group (46.1%), followed by those who are single (32.8%) and divorced or separated (18.1%). Widowed individuals represent a small minority (~3.1%).
relationship
adult_df['relationship'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')relationship variable by category proportions.
| unique values | proportion | |
|---|---|---|
| 0 | male-spouse | 0.405315 |
| 1 | single | 0.360686 |
| 2 | own-child | 0.155599 |
| 3 | female-spouse | 0.048227 |
| 4 | extended-relative | 0.030173 |
The majority are labeled as “male spouse” (40.5%) or “single” (36.1%). Smaller categories include children (15.6%), female spouses (4.8%), and extended relatives (3.0%). The dominance of male spouse reflects the dataset’s gendered structure and may point to traditional family roles. The relative scarcity of “female spouse” roles suggests potential gender imbalances in how income-earning is reported within households.
race
adult_df['race'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')race variable categories.
| unique values | proportion | |
|---|---|---|
| 0 | white | 0.854151 |
| 1 | black | 0.096023 |
| 2 | asian or pacific islander | 0.031926 |
| 3 | american indian or eskimo | 0.009565 |
| 4 | other | 0.008335 |
The dataset is overwhelmingly composed of White individuals (~85.4%). Other racial groups include Black (9.6%), Asian or Pacific Islander (3.2%), American Indian or Eskimo (1.0%), and Other (0.8%). The racial imbalance limits the generalizability of models trained on this data. Smaller racial groups may suffer from limited statistical power, affecting fairness and performance in predictive modeling.
sex
adult_df['sex'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')sex variable within the dataset.
| unique values | proportion | |
|---|---|---|
| 0 | male | 0.669209 |
| 1 | female | 0.330791 |
Males constitute 66.9% of the dataset, with females making up the remaining 33.1%. This male-skewed distribution could be due to sampling (e.g., primary earners in households), workforce participation patterns, or reporting biases.
education_level
adult_df['education_level'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')education_level) by proportion.
| unique values | proportion | |
|---|---|---|
| 0 | secondary-school-graduate | 0.322456 |
| 1 | tertiary | 0.247809 |
| 2 | some-college | 0.223787 |
| 3 | secondary | 0.093932 |
| 4 | associate | 0.075324 |
| 5 | primary | 0.030050 |
| 6 | unknown | 0.005106 |
| 7 | preschool | 0.001538 |
Secondary-school graduates form the largest educational group (~32%), highlighting the central role of high school completion in the labor force. Tertiary education holders — those with university or equivalent degrees — account for nearly 25% of the population, representing a substantial segment with advanced qualifications. A notable 22.4% have attended some college without necessarily earning a degree, suggesting that partial post-secondary education is common, yet may not always translate into formal certification. The remaining 20% are distributed among those with only secondary education (9.4%), associate degrees (7.5%), primary school (3.5%), and a very small group with only preschool education (0.15%). It is ecident that the education distribution is skewed toward mid- to high-level education, with relatively few individuals having only basic schooling. This reflects a dataset that largely captures working-age adults in formal labor, which may underrepresent the least-educated populations.
occupation_grouped
adult_df['occupation_grouped'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')occupation_grouped) in the dataset.
| unique values | proportion | |
|---|---|---|
| 0 | white collar | 0.508474 |
| 1 | blue collar | 0.308861 |
| 2 | service | 0.105742 |
| 3 | unknown | 0.056685 |
| 4 | Service | 0.019961 |
| 5 | millitary | 0.000277 |
White-collar occupations are the most prevalent (~51%), followed by blue-collar, service, and unknown. Smaller categories include military, which is marginal. Essentially, slightly over half of individuals in the dataset work in professional, managerial, sales, clerical, or tech-support roles. This suggests the dataset is heavily weighted toward professional and administrative occupations. Nearly a third of the population works in manual labor or skilled trade positions (craft, transport, machine operation, farming, etc.). This indicates a significant segment engaged in physically intensive or technical labor.
native_region
adult_df['native_region'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')native_region categories by proportion.
| unique values | proportion | |
|---|---|---|
| 0 | north america | 0.923261 |
| 1 | asia | 0.020638 |
| 2 | other | 0.017870 |
| 3 | central america | 0.016117 |
| 4 | europe | 0.016024 |
| 5 | south america | 0.006090 |
The vast majority of individuals are from North America (~92.3%). Smaller proportions are from Central America, Asia, Europe, South America, and a generic Other category. The heavy concentration of North American individuals reflects the U.S. focus of the dataset.
age_group
adult_df['age_group'].value_counts(normalize=True).rename_axis('unique values').reset_index(name='proportion')age_group categories in the dataset.
| unique values | proportion | |
|---|---|---|
| 0 | 26-35 | 0.261465 |
| 1 | 36-45 | 0.246086 |
| 2 | 46-60 | 0.224156 |
| 3 | 18-25 | 0.167533 |
| 4 | 61-75 | 0.064313 |
| 5 | <18 | 0.029065 |
| 6 | 76+ | 0.007382 |
The largest groups are 26–35 and 36–45, followed by 46–60. These three age groups represent about 73% of the dataset. Very few individuals are under 18 or above 75, consistent with the dataset’s focus on the working-age population.
Given that income is the target variable, most of the analysis hereafter will be based on it. We first of all examine the income distribution in the dataset.
fig = px.pie(adult_df_income, names='income', values='total', title='Overall Income Distribution', color_discrete_sequence=['seagreen','lightgreen'])
fig.update_layout(template="presentation", legend_title=dict(text='Income Level'), paper_bgcolor = "rgba(0, 0, 0, 0)", plot_bgcolor = "rgba(0, 0, 0, 0)")
fig.show()
fig.write_image(os.path.join(results_dir, 'income_distribution_pie_chart.jpg'))
fig.write_image(os.path.join(results_dir, 'income_distribution_pie_chart.png'))
fig.write_html(os.path.join(results_dir, 'income_distribution_pie_chart.html'))This pie chart visualizes the overall income split: 76% of individuals earn ≤50K, while 24% earn >50K. This means that nearly 3 out of 4 individuals fall into the lower income bracket (<=50K). This shows that there is a significant imbalance.
fig = px.bar(
adult_df_income_age,
x = 'age_group',
y = 'percentage',
color = 'income',
title = 'Income Distribution by Age Group (%)',
barmode = 'group',
height = 600,
width=1000,
color_discrete_sequence=['seagreen','lightgreen'],
text= 'percentage'
)
fig.update_traces(texttemplate='%{text:.2f}%', textposition='outside')
fig.update_layout(template="presentation", xaxis_title='Age Group', margin=dict(l=100, r=50, t=50, b=150),
yaxis_title='Percentage of population', legend_title=dict(text='Income Level'),
paper_bgcolor = "rgba(0, 0, 0, 0)", plot_bgcolor = "rgba(0, 0, 0, 0)")
fig.show()
fig.write_image(os.path.join(results_dir, 'income_distribution_by_agegroup_bar_plot.jpg'))
fig.write_image(os.path.join(results_dir, 'income_distribution_by_agegroup_bar_plot.png'))
fig.write_html(os.path.join(results_dir, 'income_distribution_by_agegroup_bar_plot.html'))The bar chart visualizes the income distribution across age groups, using percentages within each group. There is an evident pattern in terms of income progression over the years with a gradual increase in terms of the number of people earning >50K starting from 0 amongst those aged 18 and below, peaking between 36 and 60 years, then declining after 60 years but not to zero.
All individuals under 18 earn <=50K, likely due to being students, minors, or ineligible for full-time employment. Extremely few young adults (2.1%) exceed 50K, as most are early in their careers, pursuing education, or in entry-level jobs. For the 26-35 age group, there’s a noticeable improvement — roughly 1 in 5 individuals in this group earn >50K, reflecting early career progression and accumulation of qualifications/experience. A substantial income increase is seen in the 36-45 age group: over a third now earn >50K. This is typically considered prime earning age where individuals settle into stable, higher-paying positions. Highest proportion of >50K earners is seen amongst individuals aged between 46 and 60— nearly 4 in 10. This reflects career maturity, peak seniority levels, and accumulated experience. There’s a drop-off in high incomes as many transition to retirement, part-time, or less demanding roles in the age group 61-75. Yet about 1 in 4 still earn >50K. Most in 76+ age group earn <=50K, likely due to retirement, pensions, or fixed incomes — but a small minority still earn higher incomes, possibly through continued work or investments.
fig = px.bar(
adult_df_income_reg,
x = 'native_region',
y = 'percentage',
color = 'income',
title = 'Income Distribution by Native Region (%)',
barmode = 'group',
height = 600,
width=1000,
color_discrete_sequence=['seagreen','lightgreen'],
text= 'percentage'
)
fig.update_traces(texttemplate='%{text:.2f}%', textposition='outside')
fig.update_layout(template="presentation", xaxis_title='Native Region', yaxis_title='Percentage of population', margin=dict(l=100, r=50, t=50, b=150), legend_title=dict(text='Income Level'),
xaxis_title_standoff=50, paper_bgcolor = "rgba(0, 0, 0, 0)", plot_bgcolor = "rgba(0, 0, 0, 0)")
fig.show()
fig.write_image(os.path.join(results_dir, 'income_distribution_by_nativeregion_bar_plot.jpg'))
fig.write_image(os.path.join(results_dir, 'income_distribution_by_nativeregion_bar_plot.png'))
fig.write_html(os.path.join(results_dir, 'income_distribution_by_nativeregion_bar_plot.html'))Asia (30.7%) and Europe (29.2%) have the highest proportions of high-income earners. This suggests these immigrant groups might be better integrated into high-paying professional roles, or may represent a more skilled migrant profile in the dataset. Central America (11.1%) and South America (12.1%) have the lowest proportions of >50K earners. With 24.2% of North Americans earning >50K, this serves as a middle-ground baseline. Interestingly, both Asian and European groups outperform the native-born population proportionally in high-income brackets. The ‘Other’ group sits around 25.1%, close to North America’s rate. This likely reflects a diverse mix of regions not explicitly listed.
fig = px.bar(
adult_df_income_race,
x = 'race',
y = 'percentage',
color = 'income',
title = 'Income Distribution by Race (%)',
barmode = 'group',
height = 700,
width=1000,
color_discrete_sequence=['seagreen','lightgreen'],
text= 'percentage'
)
fig.update_traces(texttemplate='%{text:.2f}%', textposition='outside')
fig.update_layout(template="presentation", xaxis_title='Race', yaxis_title='Percentage of population', legend_title=dict(text='Income Level'),
xaxis_title_standoff=30, margin=dict(l=60, r=50, t=50, b=150), paper_bgcolor = "rgba(0, 0, 0, 0)", plot_bgcolor = "rgba(0, 0, 0, 0)")
fig.show()
fig.write_image(os.path.join(results_dir, 'income_distribution_by_race_bar_plot.jpg'))
fig.write_image(os.path.join(results_dir, 'income_distribution_by_race_bar_plot.png'))
fig.write_html(os.path.join(results_dir, 'income_distribution_by_race_bar_plot.html'))Asian or Pacific Islander (26.6%) and White (25.6%) populations have the highest proportions of >50K earners. Asians/Pacific Islanders marginally outperform Whites, a pattern often attributed to occupational concentration in high-paying sectors like technology and medicine. On the other hand, American Indian or Eskimo (11.6%), Black (12.4%), and Other (9.2%) groups show significantly lower rates of high-income earners. These figures reflect long-standing economic disparities rooted in historical exclusion, occupational segregation, and systemic inequality.
The stark differences in high-income proportions:
These disparities are consistent with well-documented wage gaps and underrepresentation of marginalized groups in higher-paying roles.
num = 15
adult_df_combos = adult_df_income_edu_occ.head(num)
fig = px.bar(
adult_df_combos,
x = 'total',
y = 'edu_occ',
color = 'income',
orientation = 'h',
title = f'Top {num} Education and Occupation Groups Combinations by Income Group',
# barmode = 'group',
height = 500,
width=1100,
color_discrete_sequence=['seagreen','lightgreen'],
text = 'total'
)
fig.update_layout(template="presentation",
xaxis_title='Number of Individuals',
yaxis_title='Education | Occupation Group',
legend_title=dict(text='Income Level'),
margin=dict(l=450, r=50, t=50, b=50),
paper_bgcolor = "rgba(0, 0, 0, 0)",
plot_bgcolor = "rgba(0, 0, 0, 0)")
fig.update_traces(textposition='inside')
fig.show()
fig.write_image(os.path.join(results_dir, 'income_distribution_by_eduandocc_bar_plot.jpg'))
fig.write_image(os.path.join(results_dir, 'income_distribution_by_eduandocc_bar_plot.png'))
fig.write_html(os.path.join(results_dir, 'income_distribution_by_eduandocc_bar_plot.html'))From the bar chart, we can pick out the largest groups per income-level. We see that secondary-school graduates working a blue collar job occupy the largest group in the dataset (3976). This reflects a common socio-economic profile: individuals with basic schooling in manual or technical trades predominantly earning lower incomes. The largest high-income group are tertiary-educated individuals in white collar roles. This highlights the strong earning advantage conferred by higher education and skilled jobs.
Some of the key patterns we can get from the dataset are:
Tertiary education combined with white-collar work offers the highest income prospects. Yet a substantial number of tertiary-educated white-collar workers earn <=50K, likely early career, part-time, or structural pay gaps.
Even some college education doesn’t guarantee high incomes in these sectors. Manual and service sector income is highly occupation-dependent (some skilled trades can break the 50K mark).
Secondary-school graduates in blue-collar and white-collar work have decent representation among >50K earners. This reflects upward mobility possible through skilled trades, tenure, or niche roles.